using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GraphFS
{

	public class FSNodeList : Dictionary<Int64, Int64>
	{
		public FSNodeList()
		{

		}

		public void Read(string file)
		{

			FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read,FileShare.ReadWrite);
			BinaryReader br = new BinaryReader(fs);
			Int64 nodeId = -1;
			Int64 nodeOffset = -1;
			while (fs.Position < fs.Length)
			{
				nodeId=br.ReadInt64();
				nodeOffset=br.ReadInt64();
				this.Add(nodeId, nodeOffset);
			}
			br.Close();
			
		}

		public void Write(string file)
		{
			if (File.Exists(file))
			{
				File.Delete(file);
			}

			FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
			BinaryWriter bw = new BinaryWriter(fs);
			foreach (KeyValuePair<Int64, Int64> kvp in this)
			{
				bw.Write(kvp.Key);	  //write wayId
				bw.Write(kvp.Value);  //write offset
			}
			bw.Flush();
			bw.Close();
		}







		private BinaryReader dataStream=null;



		public FSNode GetNode(Int64 nodeId)
		{
			if (!this.ContainsKey(nodeId))
				return null;


			//TODO: check dataStream open

			Int64 nodeOffset = this [nodeId];
			dataStream.BaseStream.Seek(nodeOffset, SeekOrigin.Begin);
			double lon=dataStream.ReadDouble();
			double lat = dataStream.ReadDouble();

			double nrEdges = dataStream.ReadInt32();
			for (int i = 0; i < nrEdges; i++)
			{
				Int64 edgeId=dataStream.ReadInt64();
			}

			//TODO: create caching solution for memoization // limit by size, clear by age


			return null;
		}

		
	}
}
